<template>
  <el-dialog :visible.sync="isShow" title="分配权限" width="40%">
    <el-tree
      ref="tree"
      :data="permissions"
      :props="{ label: 'name' }"
      node-key="id"
      show-checkbox
      check-strictly
    />
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="close">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/permission.js'
import { changeTreeData } from '@/utils/index.js'
import { sysRoleGet, sysRoleAssignPrem } from '@/api/setting.js'
export default {
  name: 'DialogPerm',
  data() {
    return {
      isShow: false,
      permissions: [],
      ia: ''
    }
  },
  created() {
    this.getPermissions()
  },
  methods: {
    // 获取权限列表
    async getPermissions() {
      const res = await sysPermission()
      this.permissions = changeTreeData(res.data, '0')
    },
    // 获取角色详情
    async getRoleInfo(id) {
      this.id = id
      const res = await sysRoleGet(id)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    // 取消点击
    close() {
      this.isShow = false
      this.$refs.tree.setCheckedKeys([])
    },
    // 确定点击
    async submit() {
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('分配权限成功')
      this.isShow = false
    }
  }
}
</script>

<style></style>
